# -*- coding: utf-8 -*-

import os
import logging
import logging.config
from logging.handlers import RotatingFileHandler

# 基础路径
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
# 日志级别
LOGGER_LEVEL = logging.DEBUG

LOG_SIZE = 1048576
LOG_NAME = os.path.join(BASE_DIR, "logs", "service.log")
LOG_FILES = 10


def getLogger(name):
    formatter = logging.Formatter(
        '[%(asctime)s] [%(levelname)s] %(name)s [%(filename)s - %(funcName)s - line %(lineno)s]: %(message)s'
    )

    logger = logging.getLogger(name)
    logger.setLevel(LOGGER_LEVEL)

    file_handler = RotatingFileHandler(filename=LOG_NAME,
                                       maxBytes=LOG_SIZE,
                                       backupCount=LOG_FILES,
                                       encoding='utf-8')
    file_handler.setLevel(level=LOGGER_LEVEL)
    file_handler.setFormatter(formatter)

    cmd_handler = logging.StreamHandler()
    cmd_handler.setFormatter(formatter)
    # cmd_handler.setLevel(level=LOGGER_LEVEL)

    logger.addHandler(file_handler)
    logger.addHandler(cmd_handler)

    return logger


